package mk.com.ldm.corepoint.implementation.client.lib.wrappers.widgets;

import mk.com.ldm.corepoint.implementation.client.lib.generators.BaseWidget;
import mk.com.ldm.corepoint.implementation.client.presenter.EventHandler;
import mk.com.ldm.corepoint.implementation.shared.FormRow;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.Button;

public class ButtonWrapper extends BaseWidget {

	private Button button = new Button();
	private String name = new String();
	
	public ButtonWrapper(FormRow config, HandlerManager eventBus) {
		super(config);
		
		this.setType(0);
		button.setTitle(config.getNam().replace(":",""));
		this.setName(config.getNam());
		button.setWidth(config.getSiz()+"px");
		button.setTabIndex(config.getTab());
		if(config.getEna() == 0) button.setEnabled(false);
		if(config.getVis() == 0) button.setVisible(false);
		if(!config.getSiz().equals("")){
			button.setWidth(config.getSiz().split(",")[0]+"px");
			if(config.getSiz().split(",").length > 1){
				button.setHeight(config.getSiz().split(",")[1]+"px");
			}
		}
		if(!config.getEts().isEmpty()){
			setEvents(config.getEts());
		}
		
		initWidget(button);
	}
	
	public void setEvents(String events){
		String eventList[] = events.split(",");
		
		for(int i=0; i<eventList.length; i++){
			final String eventType = eventList[i].split(":")[0];
			final String eventId = eventList[i].split(":")[1];
			 
			if(eventType.equals("click")){
				button.addClickHandler(new ClickHandler(){

					@Override
					public void onClick(ClickEvent event) {
						EventHandler.getInstance().fireEvent(eventType, eventId);					
					}
					
				});
			}
			
				
		}
	}
	
	public Button getButton() {
		return button;
	}


	public void setButton(Button button) {
		this.button = button;
	}
	
	@Override
	public void setValue(String value){}
	@Override
	public String getValue(){ return null; }
	@Override
	public void setEnabled(Boolean enabled) {button.setEnabled(enabled);}
	@Override
	public void setVisible(Boolean visible) {button.setVisible(visible);}
	@Override
	public String getTitle() {return button.getTitle();}
	@Override
	public String getText() {return button.getText();}
	@Override
	public void setText(String text) { button.setText(text); }
	@Override
	public void setName(String name) { this.name = name; }
	@Override
	public String getName() { return name; }
	@Override
	public void setFocus(boolean focus) {}
		
}
